<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="400" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="所属字典" prop="dictTitle">
				<el-input v-model="form.dictTitle" disabled></el-input>
			</el-form-item>
			<el-form-item label="数据名称" prop="title">
				<el-input v-model="form.title" clearable></el-input>
			</el-form-item>
			<el-form-item label="数据值" prop="value">
				<el-input v-model="form.value" clearable></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="description">
				<el-input v-model="form.description" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="sortOrder">
				<el-input-number v-model="form.sortOrder" :min="0" :max="100" :precision="2" :step="1"   />
			</el-form-item>
			<el-form-item label="是否有效" prop="status">
				<el-switch v-model="form.status" :active-value="0" :inactive-value="-1"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增项',
					edit: '编辑项'
				},
				visible: false,
				isSaving: false,
				form: {
					id: "",
					dicTitle: "",
					title: "",
					value: "",
					description: "",
					sortOrder:0,
					status:0
				},
				rules: {
					title: [
						{required: true, message: '请输入数据名称'}
					],
					value: [
						{required: true, message: '请输入数据值'}
					],
					sortOrder: [
						{required: true, message: '请输入排序'}
					],
				},
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaving = true;
						var res;
						if(this.mode == 'add')res = await this.$API.system.dictData.save.post(this.form);
						else if(this.mode == 'edit') res = await this.$API.system.dictData.edit.post(this.form);
						this.isSaving = false;
						if(res.success){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				if(this.mode == 'edit'){
					this.form.id = data.id
					this.form.title = data.title
					this.form.value = data.value
					this.form.sortOrder = data.sortOrder
					this.form.status = data.status
					this.form.description = data.description
					this.form.dictId = data.dictId
					this.form.dictTitle = data.dictTitle
				}else{
					this.form.dictId = data.dic.id
					this.form.dictTitle = data.dic.title
				}

			}
		}
	}
</script>

<style>
</style>
